<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>产品销量分析</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .dashboard {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        @media (max-width: 768px) {
            .dashboard {
                grid-template-columns: 1fr;
            }
        }
        
        .card {
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            padding: 20px;
            transition: transform 0.3s ease;
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
        }
        
        .card h2 {
            margin-bottom: 15px;
            color: #2c3e50;
            border-bottom: 2px solid #f0f0f0;
            padding-bottom: 10px;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        
        thead {
            background-color: #3498db;
            color: white;
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
        }
        
        th {
            cursor: pointer;
            position: relative;
            user-select: none;
        }
        
        th:hover {
            background-color: #2980b9;
        }
        
        th::after {
            content: '↕';
            position: absolute;
            right: 10px;
            opacity: 0.7;
        }
        
        th.asc::after {
            content: '↑';
        }
        
        th.desc::after {
            content: '↓';
        }
        
        tbody tr:hover {
            background-color: #f8f9fa;
        }
        
        .total-row {
            font-weight: bold;
            background-color: #ecf0f1;
        }
        
        .stats {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            flex-wrap: wrap;
        }
        
        .stat-item {
            text-align: center;
            padding: 15px;
            flex: 1;
            min-width: 120px;
        }
        
        .stat-value {
            font-size: 1.8rem;
            font-weight: bold;
            color: #3498db;
        }
        
        .stat-label {
            font-size: 0.9rem;
            color: #7f8c8d;
            margin-top: 5px;
        }
        
        .chart-container {
            height: 300px;
            position: relative;
        }
        
        .highlight {
            background-color: #fff9c4;
        }
        
        footer {
            text-align: center;
            margin-top: 30px;
            padding: 20px;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>产品销量分析</h1>
            <div class="subtitle">实时监控与数据可视化</div>
        </header>
        
        <div class="dashboard">
            <div class="card">
                <h2>产品销量表格</h2>
                <table id="salesTable">
                    <thead>
                        <tr>
                            <th data-sort="id">ID</th>
                            <th data-sort="name">产品名称</th>
                            <th data-sort="sales">销量</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>2021090901</td>
                            <td>马克杯</td>
                            <td>5,000</td>
                        </tr>
                        <tr>
                            <td>2021090902</td>
                            <td>啤酒杯</td>
                            <td>5,200</td>
                        </tr>
                        <tr>
                            <td>2021090903</td>
                            <td>搪瓷杯</td>
                            <td>3,500</td>
                        </tr>
                        <tr class="total-row">
                            <td colspan="2">合计</td>
                            <td>13,700</td>
                        </tr>
                    </tbody>
                </table>
                
                <div class="stats">
                    <div class="stat-item">
                        <div class="stat-value">3</div>
                        <div class="stat-label">产品数量</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-value">13,700</div>
                        <div class="stat-label">总销量</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-value">4,567</div>
                        <div class="stat-label">平均销量</div>
                    </div>
                </div>
            </div>
            
            <div class="card">
                <h2>销量分布</h2>
                <div class="chart-container">
                    <canvas id="salesChart"></canvas>
                </div>
            </div>
        </div>
        
        <footer>
            <p>数据更新时间: 2023年10月15日 | 数据来源: 销售系统</p>
        </footer>
    </div>

    <script>
        // 表格排序功能
        document.querySelectorAll('th[data-sort]').forEach(header => {
            header.addEventListener('click', () => {
                const table = document.getElementById('salesTable');
                const tbody = table.querySelector('tbody');
                const rows = Array.from(tbody.querySelectorAll('tr:not(.total-row)'));
                const sortKey = header.getAttribute('data-sort');
                const isAsc = header.classList.contains('asc');
                
                // 清除其他排序指示器
                document.querySelectorAll('th').forEach(th => {
                    th.classList.remove('asc', 'desc');
                });
                
                // 设置当前排序指示器
                header.classList.add(isAsc ? 'desc' : 'asc');
                
                // 排序行
                rows.sort((a, b) => {
                    let aValue, bValue;
                    
                    if (sortKey === 'id') {
                        aValue = a.cells[0].textContent;
                        bValue = b.cells[0].textContent;
                    } else if (sortKey === 'name') {
                        aValue = a.cells[1].textContent;
                        bValue = b.cells[1].textContent;
                    } else if (sortKey === 'sales') {
                        aValue = parseInt(a.cells[2].textContent.replace(/,/g, ''));
                        bValue = parseInt(b.cells[2].textContent.replace(/,/g, ''));
                    }
                    
                    if (isAsc) {
                        return aValue > bValue ? -1 : aValue < bValue ? 1 : 0;
                    } else {
                        return aValue < bValue ? -1 : aValue > bValue ? 1 : 0;
                    }
                });
                
                // 重新插入行（排除合计行）
                const totalRow = tbody.querySelector('.total-row');
                tbody.innerHTML = '';
                rows.forEach(row => tbody.appendChild(row));
                tbody.appendChild(totalRow);
                
                // 添加高亮效果
                rows.forEach(row => row.classList.add('highlight'));
                setTimeout(() => {
                    rows.forEach(row => row.classList.remove('highlight'));
                }, 1000);
            });
        });
        
        // 图表初始化
        const ctx = document.getElementById('salesChart').getContext('2d');
        const salesChart = new Chart(ctx, {
            type: 'pie',
            data: {
                labels: ['马克杯', '啤酒杯', '搪瓷杯'],
                datasets: [{
                    data: [5000, 5200, 3500],
                    backgroundColor: [
                        'rgba(54, 162, 235, 0.7)',
                        'rgba(255, 99, 132, 0.7)',
                        'rgba(75, 192, 192, 0.7)'
                    ],
                    borderColor: [
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 99, 132, 1)',
                        'rgba(75, 192, 192, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'bottom',
                    },
                    tooltip: {
                        callbacks: {
                            label: function(context) {
                                const label = context.label || '';
                                const value = context.raw;
                                const total = 13700;
                                const percentage = Math.round((value / total) * 100);
                                return `${label}: ${value.toLocaleString()} (${percentage}%)`;
                            }
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>